//
// Created by liuzelin3 on 2023/5/24.
//
#pragma once

#include <cstdint>
#include <cstddef>

namespace Panda{

    template <class T,size_t Size = sizeof(T)>
    class ByteReaderWriter{
    public:
        static T ReadByte(uint8_t*byte){
            return ReadByteIndex(byte,1);
        }

        static void WriteByte(uint8_t* byte,T v) {
            WriteByteIndex(byte,0,v);
        }
    private:
        static T ReadByteIndex(uint8_t*byte,size_t i) {
            if (i == Size)
                return byte[0];
            T ret =  byte[0] << ((Size - i)*8) | ReadByteIndex(byte+1,i+1);
            return ret;
        }

        static void WriteByteIndex(uint8_t*byte,size_t i,T&v) {
            if (i == Size)
                return;
            byte[0] = v >> ((Size - i - 1) * 8);
            v = v << ((i+1)*8) >> ((i+1)*8);
            WriteByteIndex(byte+1,i+1,v);
        }
    };
}